﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using XLZF.MicroServices.IRepository;
using XLZF.MicroServices.Models;
using XLZF.MicroServices.ViewModels;

namespace XLZF.MicroServices.ClothingDictService.Controllers;

/// <summary>
/// 
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class ClothingPartsController : ControllerBase
{
    private readonly IClothingPartsRepository _IClothingPartsRepository;

    private readonly IMapper _mapper;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="iClothingPartsRepository"></param>
    /// <param name="mapper"></param>
    public ClothingPartsController(IClothingPartsRepository iClothingPartsRepository, IMapper mapper)
    {
        _IClothingPartsRepository = iClothingPartsRepository;
        _mapper = mapper;
    }

    #region 衣物品牌

    /// <summary>
    /// 查询
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("[action]", Name = "GetParts")]
    public ActionResult Get()
    {
        List<Clothing_Parts> list = _IClothingPartsRepository.GetClothingBrand();

        return Ok(list);
    }

    /// <summary>
    /// GetSearch
    /// </summary>
    /// <param name="Seachval"></param>
    /// <param name="pageIndex"></param>
    /// <param name="PageSize"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("[action]", Name = "GetSearchParts")]
    public ActionResult GetSearch(string? Seachval = "", int pageIndex = 1, int PageSize = 10)
    {
        Page<Clothing_Parts> pagedata = _IClothingPartsRepository.GetClothingPageData(Seachval, false, pageIndex, PageSize);

        var values = _mapper.Map<Page<Clothing_Parts>, Page<ClothingDictViewModel>>(pagedata);

        return Ok(values);
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Route("[action]", Name = "AddParts")]
    public ActionResult Add(string brandName)
    {
        Clothing_Parts clothing_Brand = new Clothing_Parts();

        clothing_Brand.PartsID = Guid.NewGuid().ToString();

        clothing_Brand.PartName = brandName;

        clothing_Brand.Status = 1;

        clothing_Brand.CreateBy = "1";

        clothing_Brand.CreateDate = DateTime.Now;

        return Ok(_IClothingPartsRepository.AddClothingBrand(clothing_Brand));
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="BrandID"></param>
    /// <param name="BrandName"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("[action]", Name = "UpdateParts")]
    public ActionResult Update(string BrandID, string BrandName)
    {
        var obj = _IClothingPartsRepository.GetClothingBrandSing(BrandID);

        obj.PartName = BrandName;

        obj.UpdateBy = "1";

        obj.UpdateDate = DateTime.Now;

        return Ok(_IClothingPartsRepository.EditClothingBrand(obj));
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="BrandID"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("[action]", Name = "DeleteParts")]
    public ActionResult Delete(string BrandID)
    {
        var obj = _IClothingPartsRepository.GetClothingBrandSing(BrandID);

        return Ok(_IClothingPartsRepository.DelClothingBrand(obj));
    }

    #endregion
}
